//
//  GuidePageViewController.swift
//  d11
//
//  Created by Wai Yin Chan on 28/9/2017.
//  Copyright © 2017 Tech_001. All rights reserved.
//

import Foundation
import UIKit

class GuidePageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {
    var launchViewController: LaunchViewController?
    var controllers = [UIViewController]()
    var currentPage = 0
    
    var pageControl: UIPageControl?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //根据Storyboard ID来创建一个View Controller
        let images = [
            UIImage(named: "guide_new_01"),
            UIImage(named: "guide_new_02"),
            UIImage(named: "guide_new_03"),
            UIImage(named: "guide_new_04"),
            UIImage(named: "guide_new_05"),
            UIImage(named: "guide_new_06"),
        ]
        for image in images {
            let controller = GuidePageController.instanceFromStoryboard()!
            controller.image = image
            
            if image == images[images.count-1] {
                controller.hideButton = false
            }
            
            if image == images[0] {
                controller.hideGuideSkip = false
            }
            controller.launchViewController = launchViewController
            controller.pageViewController = self
            controllers.append(controller)
        }
        
        self.setViewControllers([controllers[0]], direction: .reverse, animated: true, completion: nil)
        self.delegate = self
        self.dataSource = self
        
        /**** PageControl ****/
        pageControl = UIPageControl(frame: CGRect(x: 0,y: UIScreen.main.bounds.maxY - 50,width: UIScreen.main.bounds.width,height: 50))
        self.pageControl!.numberOfPages = controllers.count
        self.pageControl!.currentPage = 0
        self.view.addSubview(pageControl!)
        self.view.backgroundColor = UIColor.black
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if finished && completed {
            if let currentVC = pageViewController.viewControllers?.last {
                self.currentPage = controllers.index(of: currentVC)!
                self.currentPage = controllers.firstIndex(of: currentVC)!
                self.pageControl?.currentPage = self.currentPage
            }
        }
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        self.currentPage -= 1
        if self.currentPage < 0 {
            self.currentPage = 0
            return nil
        }
        return controllers[self.currentPage] as UIViewController

    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        self.currentPage += 1
        if self.currentPage >= controllers.count {
            self.currentPage = controllers.count - 1
            return nil
        }
        return controllers[self.currentPage] as UIViewController
    }
}
